<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="变更单号">
              <a-input placeholder="请输入变更单单据编号" v-model="queryParam.changeOrderNo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="采购单号">
              <a-input placeholder="请输入采购单编号" v-model="queryParam.relateOrderNo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="供应商">
              <a-input placeholder="请输入供应商关键词" v-model="queryParam.supplierName"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="收货仓库">
                <j-url-select
                  v-model="queryParam.warehouse"
                  placeholder="请选择"
                  :multiple="true"
                  url="/hzkj-wms/wms/warehouse/list?pageNo=1&pageSize=9999&type=1"
                  :optionDiy="optionWarehouseDiy"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="变更原因">
                <a-input placeholder="请输入变更原因关键词" v-model="queryParam.changeCause"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="变更人">
                <j-url-select v-model="queryParam.createBy" placeholder="请选择" url="/sys/user/getAllUsernameByOrg" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'PurchaseChangeList:export'" type="primary" icon="download" @click="handleExportXls('采购单变更表')">导出</a-button>
      <!-- 高级查询区域 -->
    </div>

    <!-- table区域-begin -->
    <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
      <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
      <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
      >项
      <a style="margin-left: 24px" @click="onClearSelected">清空</a>
    </div>

    <a-table
      ref="table"
      size="middle"
      :scroll="{ x: true }"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      @change="handleTableChange"
    >
      <template #changeNoSlot="text, record">
        <a @click="$refs.changeDetailModalRef.api_open(record.id)">{{ record.changeOrderNo }}</a>
        <a-button type="link" icon="copy" @click="copyTo(record.changeOrderNo)"></a-button>
      </template>
      <template #relateOrderNoSlot="text, record">
        <span style="color: orange">{{ record.relateOrderNo }}</span>
        <a-button type="link" icon="copy" @click="copyTo(record.relateOrderNo)"></a-button>
      </template>
      <template #productAmountSlot="text, record">
        <div class="c-red">新：{{ record.productAmountNew }}</div>
        <div class="old-size">(原：{{ record.productAmountOld }})</div>
      </template>
      <template #shippingFeeSlot="text, record">
        <div class="c-red">新：{{ record.shippingFeeNew }}</div>
        <div class="old-size">(原：{{ record.shippingFeeOld }})</div>
      </template>
      <template #totalAmountSlot="text, record">
        <div class="c-red">新：{{ formatAmount(record.productAmountNew + record.shippingFeeNew, 4) }}</div>
        <div class="old-size">(原：{{ formatAmount(record.productAmountOld + record.shippingFeeOld, 4) }})</div>
      </template>
    </a-table>
    <!-- 变更采购单详情 -->
    <change-detail-modal ref="changeDetailModalRef" />
  </a-card>
</template>

<script>
import copyToClipboard from 'copy-to-clipboard'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { formatAmount } from '@/utils/number'

import ChangeDetailModal from './modules/ChangeDetailModal.vue'

export default {
  name: 'PurchaseOrderInfoChangeList',
  mixins: [JeecgListMixin],
  components: { ChangeDetailModal },
  data() {
    return {
      formatAmount,
      // 表头
      columns: [
        {
          title: '序号',
          width: 60,
          align: 'center',
          customRender(t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '单据信息',
          align: 'center',
          width: 200,
          dataIndex: 'changeOrderNo',
          scopedSlots: { customRender: 'changeNoSlot' },
        },
        {
          title: '关联采购单',
          align: 'center',
          width: 200,
          dataIndex: 'relateOrderNo',
          scopedSlots: { customRender: 'relateOrderNoSlot' },
        },
        {
          title: '供应商',
          align: 'center',
          width: 200,
          dataIndex: 'supplierName',
          customRender(t, r) {
            return `${r.supplierName}[${r.supplierId_dictText}]`
          },
        },
        {
          title: '收货仓库',
          align: 'center',
          width: 140,
          dataIndex: 'warehouse_dictText',
        },
        {
          title: '产品总金额（元）',
          align: 'center',
          width: 160,
          scopedSlots: { customRender: 'productAmountSlot' },
        },
        {
          title: '运费（元）',
          align: 'center',
          width: 160,
          scopedSlots: { customRender: 'shippingFeeSlot' },
        },
        {
          title: '采购总金额',
          align: 'center',
          width: 160,
          scopedSlots: { customRender: 'totalAmountSlot' },
        },
        {
          title: '采购人',
          align: 'center',
          width: 100,
          dataIndex: 'purchaser',
        },
        {
          title: '变更人',
          align: 'center',
          width: 100,
          dataIndex: 'createBy',
        },
        {
          title: '变更原因',
          align: 'center',
          width: 200,
          dataIndex: 'changeCause',
        },
        {
          title: '创建时间',
          align: 'center',
          width: 160,
          dataIndex: 'createTime',
        },
      ],
      url: {
        list: '/hzkj-purchase/purchase/change/order/list',
        exportXlsUrl: '/purchase/change/order/exportXls',
      },
    }
  },
  methods: {
    // 仓库下拉框自定义
    optionWarehouseDiy({ records }) {
      return records.map((item) => {
        return {
          ...item,
          value: item.id,
          text: `${item.name}[${item.code}](${item.status == 'Y' ? '启用' : '停用'})`,
        }
      })
    },
    // 复制到剪贴板
    copyTo(value) {
      try {
        copyToClipboard(value)
        this.$message.success(`已复制单号：${value}`)
      } catch (e) {
        this.$message.error('复制失败')
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
.c-red {
  color: #ff4d4f;
}
.old-size {
  color: grey;
  text-decoration: line-through;
}
</style>
